package utlis;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * @author ldy
 * @data 2022/11/28
 * @description
 */

public class NewDateUtils {
    public static void main(String[] args) {
        System.out.println(NewDateUtils.getThisMonthLastDay(new Date()));
    }

    /**
     * 获取下个月的第一天
     * @param date
     * @return
     */
    public static Date getLastMonthFirstDay(Date date){
        ZonedDateTime dateTime=dateToZonedDateTime(date);
        ZonedDateTime endDate = dateTime.plusMonths(1L).withDayOfMonth(1).truncatedTo(ChronoUnit.DAYS).plus(-1L, ChronoUnit.MILLIS);
        return new Date(Date.from(endDate.toInstant()).getTime()+1);
    }

    /**
     * 获取当月最后一秒
     * @param date
     * @return
     */
    public static Date getThisMonthLastDay(Date date){
        ZonedDateTime dateTime=dateToZonedDateTime(date);
        ZonedDateTime endDate = dateTime.plusMonths(1L).withDayOfMonth(1).truncatedTo(ChronoUnit.DAYS).plus(-1L, ChronoUnit.MILLIS);
        return new Date(Date.from(endDate.toInstant()).getTime());
    }
    public static ZonedDateTime dateToZonedDateTime(Date date){
        ZoneId zone = ZoneId.systemDefault();
        ZonedDateTime dateTime = date.toInstant().atZone(zone);
        return dateTime;
    }
}
